package fbv.gui.site;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fbv.commun.dataTools.servico.ObjetoVO;
import fbv.commun.excecoes.FBVException;
import fbv.distribuidora.sistema.delegate.SistemaDelegate;
import fbv.distribuidora.sistema.servico.Sistema;

/**
 * Servlet implementation class for Servlet: ServletControladorListaCompra
 *
 */
public class ServletControladorListaCompra extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	static final String TORKEN_COOKIE = "##PrOdUtOs##";

	Sistema sistema = SistemaDelegate.getInstancia();

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ServletControladorListaCompra() {
		super();
	}   	

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		listarItens(request);
		
		RequestDispatcher rd = request.getRequestDispatcher("/site/confirmaCompra.jsp");
		rd.forward(request, response);
	}
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String url = "";

		if(request.getParameter("acao") != null){
			if(request.getParameter("item") != null){				

				String item = request.getParameter("item");

				if(request.getParameter("acao").equalsIgnoreCase("add")){

					addItem(response, item);
					url = "/ServletControladorDeExibicao";
				}
				else{

					removeItem(request, response, item);
					listarItens(request);
					url = "/site/listaCompra.jsp";
				}
			}
			else{
				listarItens(request);
				url = "/site/listaCompra.jsp";
			}
		}
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}  	

	private void removeItem(HttpServletRequest request, HttpServletResponse response, String item){

		Cookie c = new Cookie(TORKEN_COOKIE+item, "");
		c.setMaxAge(0);
		response.addCookie(c);
	}

	private void addItem(HttpServletResponse response, String item){

		Cookie cookie = new Cookie(TORKEN_COOKIE+item,item);
		cookie.setMaxAge(60*60*24*30);
		response.addCookie(cookie);
	}

	private void listarItens(HttpServletRequest request){

		Cookie[] cookies = request.getCookies();
		Collection<ObjetoVO> collection = new ArrayList<ObjetoVO>();

		try {
			for (Cookie cookie : cookies) {
				if(cookie.getName().length() > 12){
					if(cookie.getName().subSequence(0, 12).equals(TORKEN_COOKIE))
					{
						if(cookie.getValue() != ""){
							ObjetoVO objetoVO = sistema.consultarProduto(cookie.getValue());				
							collection.add(objetoVO); 
						}
					}
				}
			}
		} catch (FBVException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.setAttribute("produtos", collection);
	}
}